import React from 'react';
import { Input } from 'antd';

const { Search } = Input;
export default class HOCSearch extends React.PureComponent {
  constructor(props) {
    super(props);
    this.inputRef = React.createRef();
  }

  /**
   *  某个脚本会调用当前ref组件的inputRef.current.focus方法用此方法代理一下
   * @memberof HOCSearch
   */
  focus = () => {
    try {
      this.inputRef.current.input.focus();
    } catch (e) {
      console.error(e);
    }
  };

  blur = () => {
    try {
      this.inputRef.current.input.blur();
    } catch (e) {
      console.error(e);
    }
  };

  render() {
    const { props } = this;
    let hocFilterOption = null;
    const { filterOption } = props;
    // 有提供filterOption的做操作
    if (props.filterOption) {
      hocFilterOption = (input, option) => {
        const handleInput = input.replace(/[\r\n]/g, '');
        filterOption.apply(null, [handleInput, option]);
      };
    }
    return <Search ref={this.inputRef} {...props} filterOption={hocFilterOption} />;
  }
}
